package day015.differ;

import day015.RunableDemo01;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/3 0003
 */
public class ThreadAndRunable {
    public static void main(String[] args) {
//        Thread t1 = new T1();
//        Thread t2 = new T1();
//        t1.setName("window01");
//        t2.setName("window02");
//
//        t1.start();
//        t2.start();

        Runnable r = new T2();
        Thread t3 = new Thread(r);
        Thread t4 = new Thread(r);

        t3.start();
        t4.start();
    }
}

/***利用Thread - 共享代码,不共享资源**/
class T1 extends Thread{

    //资源...
    //private int ticket = 10;//卖票

    //静态资源 -
    private static int ticket = 10;

    //线程不安全的对象尽量不要设置成static
    //private StringBuilder builder = new StringBuilder();//线程不安全的

    //private static StringBuffer builder = new StringBuffer();//线程安全的

    @Override
    public void run() {
        for (int i = 0;i<100000;i++){
            System.out.println(Thread.currentThread().getName()+"正在卖票,剩余:"+ticket--+"票");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            for(int j=0;j<10000;j++)
//            builder.append(i);
        }
        //System.out.println(builder.length());
    }
}

/***Runable方式 - 共享代码,共享资源**/
class T2 implements Runnable{

    //资源...
    private int ticket = 10;//卖票

    @Override
    public void run() {
        for (int i = 0;i<100000;i++){

            System.out.println(Thread.currentThread().getName()+"正在卖票,剩余:"+ticket--+"张票!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}